package com.athuiju.fenxiaoback.service.impl;

import com.athuiju.fenxiaoback.entity.PmsSkuSaleParamValue;
import com.athuiju.fenxiaoback.entity.TbCart;
import com.athuiju.fenxiaoback.entity.TbCartitem;
import com.athuiju.fenxiaoback.entity.TbSku;
import com.athuiju.fenxiaoback.mapper.TbCartMapper;
import com.athuiju.fenxiaoback.service.PmsSkuSaleParamValueService;
import com.athuiju.fenxiaoback.service.TbCartService;
import com.athuiju.fenxiaoback.service.TbCartitemService;
import com.athuiju.fenxiaoback.service.TbSkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author athuiju
 * @since 2021-07-02
 */
@Service
public class TbCartServiceImpl extends ServiceImpl<TbCartMapper, TbCart> implements TbCartService {

    @Autowired
    TbSkuService skuService;

    @Autowired
    TbCartService cartService;

    @Autowired
    TbCartitemService cartitemService;

    @Autowired
    PmsSkuSaleParamValueService pmsSkuSaleParamValueService;


    //获取被选中的购物项
    @Override
    public List<TbCartitem> getCurrentUserCartItems(String memberid) {
        List<TbCartitem> items = cartitemService.list(new QueryWrapper<TbCartitem>().eq("memberid", memberid).eq("is_check",1));
        for (int i=0;i<items.size();i++){
            String skuId = items.get(i).getSkuId();
            TbSku sku = skuService.getById(skuId);
            items.get(i).setPrice(new BigDecimal(sku.getPrice()));
            items.get(i).setTotalPrice(new BigDecimal(sku.getPrice()*items.get(i).getCount()));

        //    得到当前sku的销售属性
            List<String> saleParamValues = pmsSkuSaleParamValueService.getSkuSaleAttrValuesAsStringList(Long.parseLong(skuId));
            items.get(i).setSkuAttr(saleParamValues);
        }
        return items;
    }

    @Transactional
    @Override
    public Integer delMemberCartItem(String memberid, String[] skuid) {
        QueryWrapper<TbCartitem> itemQuery=null;

        int count=0;
        for (int i=0;i<skuid.length;i++){
            itemQuery=new QueryWrapper<>();
//            itemQuery
            itemQuery.eq("memberid", memberid);
            itemQuery.eq("skuId",skuid[i]);
            boolean remove = cartitemService.remove(itemQuery);
            count++;
        }

        return count;
    }


    @Override
    public List<TbCartitem> selectMemberCart(String memberid) {
//        1.查询用户的购物车得到  购物车id
        QueryWrapper<TbCart> cartQueryWrapper=new QueryWrapper<>();
        cartQueryWrapper.eq("memberid", memberid);
        TbCart cart = cartService.getOne(cartQueryWrapper);

        if (cart==null){
            return new ArrayList<TbCartitem>();
        }

//        2.根据购物车id得到购物车项（每个商品）
        QueryWrapper<TbCartitem> cartItemQueryWrapper=new QueryWrapper<>();
        cartItemQueryWrapper.eq("cartid", cart.getId());
        List<TbCartitem> cartitems = cartitemService.list(cartItemQueryWrapper);


        //3.查询每一项的sku规格
        for (int i=0;i<cartitems.size();i++){
            String skuId = cartitems.get(i).getSkuId();
            List<String> values = pmsSkuSaleParamValueService.getSkuSaleAttrValuesAsStringList(Long.parseLong(skuId));
            cartitems.get(i).setSkuAttr(values);
        }


        return cartitems;
    }


    @Override
    public TbCartitem addToCart(Long skuId, Integer num,String memberid) {

        //判断当前用户是否有购物车
        TbCart cart = cartService.getOne(new QueryWrapper<TbCart>().eq("memberid", memberid));
        if (cart==null){//如果没有购物车就创建一个
            cart=new TbCart();
            cart.setCountNum(0);
            cart.setCountType(0);
            cart.setMemberid(Long.parseUnsignedLong(memberid) );
            cart.setTotalAmount(new BigDecimal("0.00"));
            cartService.save(cart);
        }

        //0.如果sku以前添加过，就只对数量进行添加
        QueryWrapper<TbCartitem> cartitemWrapper=new QueryWrapper<>();
        cartitemWrapper.eq("skuId",skuId);
        cartitemWrapper.eq("memberid",memberid);

        TbCartitem cartitem1 = cartitemService.getOne(cartitemWrapper);
        if (cartitem1!=null){
            cartitem1.setCount(cartitem1.getCount()+num);
            cartitemService.update(cartitem1,cartitemWrapper);
            return cartitem1;
        }


        //1.查询当前要添加的商品的信息
        TbSku sku = skuService.getById(skuId);

        TbCartitem cartitem=new TbCartitem();
        cartitem.setCartid(cart.getId());
        cartitem.setCheck(false);
        cartitem.setImage(sku.getImages().split(",")[0]);
        cartitem.setPrice(new BigDecimal(sku.getPrice()));
        cartitem.setTitle(sku.getTitle());
        cartitem.setSkuId(sku.getId());
        cartitem.setCount(num);
        cartitem.setMemberid(Long.parseLong(memberid));


        //List<PmsSkuSaleParamValue> skuSaleParamValues = pmsSkuSaleParamValueService.list(new QueryWrapper<PmsSkuSaleParamValue>().eq("sku_id", sku.getId()));
        List<String> values = pmsSkuSaleParamValueService.getSkuSaleAttrValuesAsStringList(skuId);
        cartitem.setSkuAttr(values);


        boolean save = cartitemService.save(cartitem);


        return cartitem;
    }

}
